<?php

namespace App\controllers;
use App\models\FdManager;
use Consts\RedisKey;
use Helper\Config;
use Helper\Output;
use Helper\FileUploader;
use Helper\Mysql;
use Helper\Redis;
use Helper\Reg;
use Imagine\Imagick\Imagine;

class Base
{
    
    protected function getUserId(){
        return Reg::getInstance()->get('user_id');
    }
    protected function getUser(){

        return Reg::getInstance()->get('user');
    }
    protected function getRoleId(){

        return Reg::getInstance()->get('user')->role_id;
    }



    public function getFdByUid( $uid ){

        $fd = Redis::getInstance()->hGet(RedisKey::WS_CUSTOMER_UID_TO_FD,$uid);

        return $fd;

    }


    public function getFdByAuid( $auid ){

        $fd = Redis::getInstance()->hGet(RedisKey::WS_SERVICE_AUID_TO_FD,$auid);

        return $fd;

    }


    /**
     * 上传图片
     * @param $request
     * @param string $formKey
     * @return bool
     */
    protected function uploadImgs($request,$formKey = "img",$imgType = ""){

        $filesystem = Config::getInstance()->get($imgType?:'filesystem');

        if($request->files[$formKey]){

            FileUploader::setTypes($filesystem['ext']);
            $fileName = FileUploader::uploadOne($request->files[$formKey],$filesystem['img_path'],$filesystem['size']);
            if(!$fileName){
                return false;
            }
            $imagine = new Imagine();
            $imagine->open($filesystem['img_path'].'/'.$fileName)->save($filesystem['img_path'].'/'.$fileName, array('jpeg_quality' => 50));
            return  $fileName;

        }

        return  false;
    }


    /**
     * 上传视频
     * @param $request
     * @param string $formKey
     * @return bool
     */
    protected function uploadVideos($request,$formKey = "video"){

        $filesystem = Config::getInstance()->get('videosystem');

        if($request->files){
            FileUploader::setTypes($filesystem['ext']);
            $fileName = FileUploader::uploadOne($request->files[$formKey],$filesystem['video_path'],$filesystem['size']);
            if(!$fileName){
                return false;
            }
            return  $fileName;

        }

        return  false;
    }





}